from django.template import loader, RequestContext
from django.shortcuts import render_to_response, get_object_or_404
from django.utils.translation import ugettext_lazy as _
from models import Event
import datetime

def display_month(request, year=None, month=None, mode=None):
    auto_date = False
    
    if mode is _('list'):
        mode = 'list'
    else:
        mode = 'calendar'
        
    try:
        year = int(year)
        month = int(month)
    except TypeError:
        auto_date = True
        year = datetime.datetime.today().year
        month = datetime.datetime.today().month
    
    current = datetime.datetime(year, month, 1)
    next = datetime.datetime(year + (month is 12), 1 + month % 12, 1)
    prev = datetime.datetime(year - (month is 1),  1 + (month+10) % 12, 1)
    
    try:
        events = Event.objects.filter(date_start__year=year, date_start__month=month)
    except Event.DoesNotExist:
        events = False
    
    return render_to_response('events/%s.html' % mode, {
        'auto_date': auto_date,
        'events': events,
        'current_month': current,
        'next_month': next,
        'previous_month': prev,
    }, RequestContext(request))